package mortal.game.control
{
    import Message.Public.*;
    import com.greensock.*;
    import flash.utils.*;
    import modules.*;
    import mortal.common.sound.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.skill.*;
    import mortal.game.view.system.*;
    import mortal.game.view.uiIconBtn.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class SkillController extends Controller
    {
        private var _skillHintIcon:SkillHintIcon;
        private var _tweenIn:TweenMax;
        private var _learnReq:Boolean;
        private var _timerKey:uint;

        public function SkillController()
        {
            
        }

        public function get hintIcon() : SkillHintIcon
        {
            return this._skillHintIcon;
        }

        public function showHintIcon() : void
        {
            if (cache.guide.skillLearnAlert == true)
            {
                this.hideHintIcon();
                
            }
            if (cache.guide.guideSkillLearn == true)
            {
                this.hideHintIcon();
                
            }
            if (!view.isHide)
            {
                
            }
            if (!this._skillHintIcon)
            {
                this._skillHintIcon = new SkillHintIcon();
            }
            this._skillHintIcon.show();
            UiHintIconView.instance.addHintIcon(this._skillHintIcon);
            
        }

        public function hideHintIcon() : void
        {
            if (this._skillHintIcon)
            {
                if (this._skillHintIcon.parent)
                {
                    this._skillHintIcon.parent.removeChild(this._skillHintIcon);
                    EffectManager.glowFilterUnReg(this._skillHintIcon);
                    this._skillHintIcon.dispatchEvent(new UIIconEvent(UIIconEvent.RemoveIcon));
                }
            }
            
        }

        private function onSkillLearnReq(event:DataEvent) : void
        {
            clearTimeout(this._timerKey);
            this._learnReq = true;
            setTimeout(this.onTimerOutHandler, 1000);
            GameProxy.skillProxy.skillLearnReq(event.data as int);
            
        }

        private function onSkillUpGradeReq(event:DataEvent) : void
        {
            clearTimeout(this._timerKey);
            this._learnReq = true;
            setTimeout(this.onTimerOutHandler, 1000);
            GameProxy.skillProxy.skillUpgradeReq(event.data as int);
            
        }

        private function onTimerOutHandler() : void
        {
            this._learnReq = false;
            
        }

        private function onSkillListRes(param1:Array) : void
        {
            if (cache.role.roleInfo.career == ECareer._ECareerNo || cache.role.roleInfo.career == ECareer._ECareerNewPlayer)
            {
            }
            else
            {
                if (_view != null)
                {
                    (_view as ISkillModule).initSkill(param1);
                }
                this.guideLevelUpAble();
            }
            
        }

        private function onSkillUpdate(param1:SkillInfo) : void
        {
            var _loc_2:String = null;
            this._learnReq = false;
            cache.guide.skillLearnAlert = false;
            SoundManager.instance.soundPlay(SoundTypeConst.LearnSkill);
            this.guideLevelUpAble();
            this.guideLevelUp();
            if (param1.position < 2 && param1.learned && param1.tSkill.skillLevel == 1)
            {
                param1.autoUse = true;
                SystemSetter.localSetter.skillIdArr[param1.position] = 1;
                _loc_2 = SystemSetter.localSetter.getString();
                Dispatcher.dispatchEvent(new DataEvent(EventName.Sys_SaveInfoNotShowInWindow, _loc_2));
            }
            if (_view != null)
            {
                (_view as ISkillModule).updateSkill(param1);
            }
            if (cache.skill.skillLearnAble.length == 0)
            {
                if (_view != null && !_view.isHide)
                {
                    _view.hide();
                }
            }
            
        }

        private function updateCondition(param1:Object) : void
        {
            if (this._learnReq)
            {
                
            }
            if (_view != null)
            {
                (_view as ISkillModule).updateCondition();
            }
            this.guideLevelUpAble();
            this.guideLevelUp();
            
        }

        private function roleLevelUpdate(event:PlayerEvent) : void
        {
            this.updateCondition(null);
            
        }

        private function onGetSkillBook(event:DataEvent) : void
        {
            this.guideLevelUpAble();
            this.guideLevelUp();
            
        }

        private function guideLevelUpAble() : void
        {
            cache.skill.updateLearnAble();
            if (cache.skill.skillLearnAble.length > 0)
            {
                if (!cache.skill.skillLearnAbleChange())
                {
                    
                }
                this.showHintIcon();
            }
            else
            {
                this.hideHintIcon();
            }
            
        }

        private function guideLevelUp() : void
        {
            if (_view && !_view.isHide)
            {
                (_view as ISkillModule).guideLevelUp(cache.skill.skillLearnAble, true);
            }
            
        }

        private function onRoleCareerUpdate(param1:Object) : void
        {
            var _loc_2:* = int(param1);
            cache.skill.initSkill([]);
            if (_view && !_view.isHide)
            {
                (_view as ISkillModule).initSkill(cache.skill.skillMaps);
            }
            this.guideLevelUpAble();
            this.guideLevelUp();
            
        }

        private function onSkillCoolDown(event:DataEvent) : void
        {
            var _loc_2:* = int(event.data);
            cache.skill.skillCoolDown(_loc_2, true);
            
        }

        private function onSkillCoolDownEnd(event:DataEvent) : void
        {
            var _loc_2:* = int(event.data);
            cache.skill.skillCoolDown(_loc_2, false);
            
        }

        private function onSkillBaseRes(param1:Array) : void
        {
            var _loc_2:Object = null;
            var _loc_3:SkillInfo = null;
            if (_view != null)
            {
                (_view as ISkillModule).initBaseSkill(param1);
            }
            if (SkillConst.isSetDefaultShort())
            {
                _loc_2 = {};
                for each (_loc_3 in param1)
                {
                    
                    if (_loc_3)
                    {
                        if (_loc_3.tSkill.skillId == SkillConst.Fight)
                        {
                            _loc_2["1"] = {id:_loc_3.tSkill.skillId, code:_loc_3.tSkill.skillId};
                            continue;
                        }
                        if (_loc_3.tSkill.skillId == SkillConst.Rest)
                        {
                            _loc_2["9"] = {id:_loc_3.tSkill.skillId, code:_loc_3.tSkill.skillId};
                            continue;
                        }
                        if (_loc_3.tSkill.skillId == SkillConst.Mount)
                        {
                            _loc_2["10"] = {id:_loc_3.tSkill.skillId, code:_loc_3.tSkill.skillId};
                        }
                    }
                }
                cache.shortCuts.defaultShort = _loc_2;
            }
            
        }

        private function onSkillFishLearedRes(param1:SkillInfo) : void
        {
            if (_view != null)
            {
                (_view as ISkillModule).addBaseSkill(param1);
            }
            
        }

        private function onSysSettingRes(event:DataEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_4:SkillInfo = null;
            var _loc_3:* = SystemSetter.currentSetter.skillIdArr.length;
            while (_loc_2 < _loc_3)
            {
                
                _loc_4 = cache.skill.getSkillInfoByPosition(_loc_2);
                if (_loc_4 != null)
                {
                    _loc_4.autoUse = SystemSetter.currentSetter.skillIdArr[_loc_2] != 0;
                }
                _loc_2++;
            }
            
        }

        private function onGuideSkillLearnHandler(event:DataEvent) : void
        {
            if (view.isHide)
            {
                view.show();
            }
            else
            {
                (view as ISkillModule).guideSkillLearn();
            }
            
        }

        private function onSkillModuleShow(event:WindowEvent) : void
        {
            this.hideHintIcon();
            if (cache.guide.guideSkillLearn)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.SkillWindowShow));
                (_view as ISkillModule).guideSkillLearn();
            }
            
        }

        private function onSkillModuleClose(event:WindowEvent) : void
        {
            if (cache.guide.guideSkillLearn)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.SkillWindowClose));
            }
            this.hideHintIcon();
            cache.skill.saveLastSkillLearnAble();
            
        }

        override protected function initView() : IView
        {
            var _loc_1:* = new SkillModule();
            _loc_1.addEventListener(WindowEvent.SHOW, this.onSkillModuleShow);
            _loc_1.addEventListener(WindowEvent.CLOSE, this.onSkillModuleClose);
            return _loc_1;
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.SkillListRes, this.onSkillListRes);
            NetDispatcher.addCmdListener(ServerCommand.SkillUpdate, this.onSkillUpdate);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateCondition);
            NetDispatcher.addCmdListener(ServerCommand.RoleExpUpdate, this.updateCondition);
            NetDispatcher.addCmdListener(ServerCommand.RoleCareerUpdate, this.onRoleCareerUpdate);
            NetDispatcher.addCmdListener(ServerCommand.SkillBaseRes, this.onSkillBaseRes);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.roleLevelUpdate);
            Dispatcher.addEventListener(EventName.GetSkillBook, this.onGetSkillBook);
            Dispatcher.addEventListener(EventName.SysSettingRes, this.onSysSettingRes);
            Dispatcher.addEventListener(EventName.Guide_SkillLearn, this.onGuideSkillLearnHandler);
            Dispatcher.addEventListener(EventName.skillLearnReq, this.onSkillLearnReq);
            Dispatcher.addEventListener(EventName.skillUpgradeReq, this.onSkillUpGradeReq);
            NetDispatcher.addCmdListener(ServerCommand.SkillFishLeared, this.onSkillFishLearedRes);
            
        }

    }
}
